package vmcs.boundary;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto
//
import vmcs.controller.StoreController;
import java.awt.*;
import java.awt.event.*;
import vmcs.boundary.listener.DoorListener;

public class MachinerySimulatorPanel extends Dialog {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static final String TITLE = "Machinery Panel";
    // public static final char  CASHDISPLAY = 'a';
    // public static final char  DRINKISPLAY = 'b';
    public static final char DIALOG = 'a';
    private StoreViewer cashDisplay;
    private StoreViewer drinksDisplay;
    private Checkbox doorDisplay;
    private StoreController storeCtrl;

    public MachinerySimulatorPanel(Frame fr, StoreController sCtrl) {
        super(fr, TITLE, false);

        storeCtrl = sCtrl;

        Label lb = new Label(TITLE);
        lb.setFont(new Font("Helvetica", Font.BOLD, 24));
        lb.setAlignment(Label.CENTER);

        cashDisplay = new StoreViewer(StoreController.CASH, storeCtrl);
        drinksDisplay = new StoreViewer(StoreController.DRINK, storeCtrl);

        Panel tp = new Panel();
        tp.setLayout(new GridLayout(0, 1));
        tp.add(cashDisplay);
        tp.add(drinksDisplay);

        Panel dp = new Panel();
        doorDisplay = new Checkbox();
        doorDisplay.addItemListener(new DoorListener(storeCtrl));
        doorDisplay.setLabel("Door Locked");
        dp.add(doorDisplay);

        this.setLayout(new BorderLayout());
        this.add("North", lb);
        this.add("Center", tp);
        this.add("South", dp);
        pack();

        addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {

                storeCtrl.closeMachineryPanel();

            }
        });
    }

    public void display() {
        this.setVisible(true);
    }

    public void closeDown() {
        dispose();
        //storeCtrl.closeDown();
    }

    public StoreViewer getCashStoreDisplay() {
        return cashDisplay;
    }

    public StoreViewer getDrinksStoreDisplay() {
        return drinksDisplay;
    }

    // state = true, door is locked, panel is not active;
    // state = false, door is unlocked, panel is active;
    public void setDoorState(boolean state) {
        doorDisplay.setState(state);
        this.setActive(DIALOG, !state);

    }

    public void setActive(char cp, boolean state) {
        switch (cp) {
            case DIALOG:
                cashDisplay.setActive(state);
                drinksDisplay.setActive(state);
                doorDisplay.setEnabled(state);
                break;
        }
    }
}
